package cn.qinhao.pethome.org.controller;

import cn.qinhao.pethome.basic.until.AjaxResult;
import cn.qinhao.pethome.basic.until.PageList;
import cn.qinhao.pethome.org.domain.Dictionarydetail;
import cn.qinhao.pethome.org.query.DictionarydetailQuery;
import cn.qinhao.pethome.org.service.IDictionarydetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/detail")
@Api(value = "字典api",description="字典展示细节功能")
public class DictionarydetailController {
    //获取业务层对象
    @Resource
    private IDictionarydetailService dictionarydetailService;
    //增加或者修改
    @PutMapping
    @ApiOperation(value = "新增或修改操作" )
    public AjaxResult addOrUpdate(@RequestBody Dictionarydetail dictionarydetail){
            if (dictionarydetail.getId()==null){
                try{
                    dictionarydetailService.add(dictionarydetail);
                    return AjaxResult.me().setSuccess(true).setMessage("添加成功");
                } catch (Exception e) {
                    e.printStackTrace();
                    return AjaxResult.me().setSuccess(false).setMessage("添加失败");
                }
            } else {
                try{
                    dictionarydetailService.update(dictionarydetail);
                    return AjaxResult.me().setSuccess(true).setMessage("修改成功");
            } catch(Exception e) {
                    e.printStackTrace();
                    return AjaxResult.me().setSuccess(false).setMessage("修改失败");
            }
        }
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult  delete(@PathVariable("id") Long id){
        try{
            dictionarydetailService.delete(id);
            return AjaxResult.me().setSuccess(true).setMessage("删除成功");
        }catch (Exception e ){
            return AjaxResult .me() .setSuccess(false ).setMessage("删除失败");
        }

    }
    //通过id传数据
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public List<Dictionarydetail> selectOne(@PathVariable("id") Long id){
        List<Dictionarydetail> dictionarydetails = dictionarydetailService.selectById(id);
        return dictionarydetails;
    }
    //查询多条数据
    @GetMapping
    @ApiOperation(value = "查询所有数据" )
    public List<Dictionarydetail> selectAll(){
        return dictionarydetailService.selectAll();
    }
    //分页
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Dictionarydetail> queryPage(@RequestBody DictionarydetailQuery query){

        return  dictionarydetailService.pageQuery(query);
    }
    //批量删除
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try{
            dictionarydetailService.batchDelete(ids);
            return AjaxResult.me().setSuccess(true).setMessage("批量删除成功");
        }catch (Exception e ){
            return AjaxResult .me() .setSuccess(false ).setMessage("网络繁忙");
        }

    }
}
